package com.yunji.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunji.news.entity.PaymentRecord;
import com.yunji.news.mapper.PaymentRecordMapper;
import com.yunji.news.service.PaymentRecordService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 缴费记录服务实现类
 *
 * @author yunji
 * @since 2024-11-26
 */
@Service
public class PaymentRecordServiceImpl extends ServiceImpl<PaymentRecordMapper, PaymentRecord> 
        implements PaymentRecordService {

    @Override
    public IPage<PaymentRecord> getPaymentPage(Integer page, Integer size, 
                                              Long schoolId, String paymentType, 
                                              String paymentStatus, String keyword) {
        Page<PaymentRecord> pageParam = new Page<>(page, size);
        return baseMapper.selectPaymentPage(pageParam, schoolId, paymentType, paymentStatus, keyword);
    }

    @Override
    public boolean createPaymentRecord(PaymentRecord record) {
        record.setPaymentStatus("pending");
        record.setStatus(1);
        return save(record);
    }

    @Override
    public boolean confirmPayment(Long id, String transactionId, String receiptUrl) {
        LambdaUpdateWrapper<PaymentRecord> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(PaymentRecord::getId, id)
               .set(PaymentRecord::getPaymentStatus, "paid")
               .set(PaymentRecord::getPaymentTime, LocalDateTime.now())
               .set(PaymentRecord::getTransactionId, transactionId)
               .set(PaymentRecord::getReceiptUrl, receiptUrl);
        return update(wrapper);
    }

    @Override
    public boolean applyRefund(Long id, BigDecimal refundAmount, String reason) {
        LambdaUpdateWrapper<PaymentRecord> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(PaymentRecord::getId, id)
               .set(PaymentRecord::getRefundStatus, "applying")
               .set(PaymentRecord::getRefundAmount, refundAmount)
               .set(PaymentRecord::getRemark, reason);
        return update(wrapper);
    }

    @Override
    public boolean confirmRefund(Long id) {
        LambdaUpdateWrapper<PaymentRecord> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(PaymentRecord::getId, id)
               .set(PaymentRecord::getRefundStatus, "refunded")
               .set(PaymentRecord::getRefundTime, LocalDateTime.now())
               .set(PaymentRecord::getPaymentStatus, "refunded");
        return update(wrapper);
    }

    @Override
    public List<PaymentRecord> getStudentPayments(String idCard, Long schoolId) {
        LambdaQueryWrapper<PaymentRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PaymentRecord::getIdCard, idCard)
               .eq(schoolId != null, PaymentRecord::getSchoolId, schoolId)
               .orderByDesc(PaymentRecord::getCreatedAt);
        return list(wrapper);
    }

    @Override
    public List<PaymentRecord> getUnpaidStudents(Long schoolId, Long majorId) {
        LambdaQueryWrapper<PaymentRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(schoolId != null, PaymentRecord::getSchoolId, schoolId)
               .eq(majorId != null, PaymentRecord::getMajorId, majorId)
               .eq(PaymentRecord::getPaymentStatus, "pending")
               .orderByDesc(PaymentRecord::getCreatedAt);
        return list(wrapper);
    }
}